#ifndef LADDERLINECARRIER_HPP
#define LADDERLINECARRIER_HPP

#include "LadderLine.hpp"
#include <QString>
#include <QList>
#include <QJsonObject>
#include <QJsonArray>
#include <QMetaType>
#include <QDebug>
#include <QUuid> // 引入 QUuid 以生成 UUID

// PointTrigger 结构体定义
struct PointTrigger {
    QString id;
    int point;
    double trigger;

    // 无参构造函数，自动生成 UUID
    PointTrigger()
        : id(QUuid::createUuid().toString()), point(0), trigger(0.0) {}

    // 有参构造函数，自动生成 UUID
    PointTrigger(int p, double t)
        : id(QUuid::createUuid().toString()), point(p), trigger(t) {}

    // 将 PointTrigger 转换为 JSON 对象
    QJsonObject toJson() const {
        QJsonObject obj;
        obj["id"] = id;
        obj["point"] = point;
        obj["trigger"] = trigger;
        return obj;
    }

    // 比较运算符，用于判断两个 PointTrigger 是否相同
    bool operator==(const PointTrigger& other) const {
        return id == other.id && point == other.point && trigger == other.trigger;
    }
};

// 重载 operator<< 以支持 QDebug 打印 PointTrigger 对象
inline QDebug operator<<(QDebug debug, const PointTrigger &pt) {
    QJsonObject obj = pt.toJson();
    debug.nospace() << "PointTrigger("
                    << "id: " << obj["id"].toString() << ", "
                    << "point: " << obj["point"].toInt() << ", "
                    << "trigger: " << obj["trigger"].toDouble()
                    << ")";
    return debug.space();
}

// LadderLineCarrier 结构体定义
struct LadderLineCarrier {
    QString id;
    QString seriesName;               // 系列名称，对应于 'node'
    double initialYOffset;            // 新增字段，默认值为 0.0
    QList<PointTrigger> dataPoints;   // dataPoints 列表

    // 无参构造函数，自动生成 UUID
    LadderLineCarrier()
        : id(QUuid::createUuid().toString()), seriesName(""), initialYOffset(0.0), dataPoints() {}

    // 有参构造函数，自动生成 UUID
    LadderLineCarrier(const QString& name, const QList<PointTrigger>& points, double yOffset = 0.0)
        : id(QUuid::createUuid().toString()), seriesName(name), initialYOffset(yOffset), dataPoints(points) {}

    // 将 LadderLineCarrier 转换为 JSON 对象
    QJsonObject toJson() const {
        QJsonObject obj;
        obj["id"] = id;
        obj["seriesName"] = seriesName;
        obj["initialYOffset"] = initialYOffset;  // 添加 initialYOffset 字段

        QJsonArray dataPointsArray;
        for (const PointTrigger &pt : dataPoints) {
            dataPointsArray.append(pt.toJson());
        }
        obj["dataPoints"] = dataPointsArray;

        return obj;
    }

    // 比较运算符，用于判断两个 LadderLineCarrier 是否相同
    bool operator==(const LadderLineCarrier& other) const {
        return id == other.id &&
               seriesName == other.seriesName &&
               initialYOffset == other.initialYOffset &&  // 比较 initialYOffset 字段
               dataPoints == other.dataPoints;
    }
};

// 重载 operator<< 以支持 QDebug 打印 LadderLineCarrier 对象
inline QDebug operator<<(QDebug debug, const LadderLineCarrier &carrier) {
    QJsonObject obj = carrier.toJson();
    debug.nospace() << "LadderLineCarrier("
                    << "id: " << obj["id"].toString() << ", "
                    << "seriesName: " << obj["seriesName"].toString() << ", "
                    << "initialYOffset: " << obj["initialYOffset"].toDouble() << ", "
                    << "dataPoints: [";

    QJsonArray dataPointsArray = obj["dataPoints"].toArray();
    for (int i = 0; i < dataPointsArray.size(); ++i) {
        QJsonObject ptObj = dataPointsArray[i].toObject();
        debug.nospace() << "{id: " << ptObj["id"].toString()
                        << ", point: " << ptObj["point"].toInt()
                        << ", trigger: " << ptObj["trigger"].toDouble() << "}";
        if (i != dataPointsArray.size() - 1) {
            debug.nospace() << ", ";
        }
    }

    debug.nospace() << "]"
                    << ")";
    return debug.space();
}

// 注册到元对象系统（如在 QVariant、信号槽中使用时）
Q_DECLARE_METATYPE(PointTrigger)
Q_DECLARE_METATYPE(LadderLineCarrier)

#endif // LADDERLINECARRIER_HPP
